package cn.tedu.trading_platform.controller;

import cn.tedu.trading_platform.base.response.JsonResult;
import cn.tedu.trading_platform.base.response.StatusCode;
import cn.tedu.trading_platform.mapper.CartMapper;
import cn.tedu.trading_platform.pojo.dto.cartDTO.AddCartParam;
import cn.tedu.trading_platform.pojo.vo.CartVO;
import cn.tedu.trading_platform.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/cart/")
@Api(tags = "购物车管理模块")
public class CartController {
    @Autowired
    private CartMapper cartMapper;
    @PostMapping("add")
    @ApiOperation("添加购物车")
    @ApiOperationSupport(order=1)
    public JsonResult addCart(@Validated @RequestBody AddCartParam addCartParam, @ApiIgnore HttpSession session)
    {
        log.debug("开始处理添加购物车请求");
        UserVO userVO = (UserVO) session.getAttribute("user");
        if(userVO==null){
            return new JsonResult(StatusCode.USER_NOT_LOGIN);
        }
        addCartParam.setUserId(userVO.getId());
        addCartParam.setUpdatedAt(new Date());
        cartMapper.insertCart(addCartParam);
        return JsonResult.ok();
    }
    @PostMapping("select")
    @ApiOperation("展示所有购物车信息")
    @ApiOperationSupport(order=2)
    public JsonResult selectCart(HttpSession session){
        log.debug("开始处理展示所有购物车信息请求");
        UserVO userVO = (UserVO) session.getAttribute("user");
        if(userVO==null){
            return new JsonResult(StatusCode.USER_NOT_LOGIN);
        }
        List<CartVO> cartVOS = cartMapper.selectAllCart(userVO.getId());
        return JsonResult.ok(cartVOS);
    }
}
